<!--  -->
<template>
    <div v-show="isShow">
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" :rules="rules" label-width="70px">
                <el-form-item label="名称" prop="itemName" required>
                    <el-input v-model="form.itemName"></el-input>
                </el-form-item>
                <el-form-item label="描述" prop="description">
                    <el-input v-model="form.description"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="close">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
           
        </el-dialog>
    </div>
</template>

<script>
import appMain from "../../utils/app_main.js";
import _ from "lodash"
export default {
    
    props: {
        isShow: false,
        isEdit: false,
        currentItem: {},
        editData: {}
    },
    data () {
        return {
            showDialog: false,
            editVisible: false,
            form: {
                itemName: '',
                description: '',
            },
            rules: {
                itemName: [{ required: true, message: '请输入内容', trigger: 'blur' }],
           }
        }
    },
    methods:  {
        close () {
            this.$emit('close', {
                showDialog: false
            })
        },
        saveEdit () {
            let self = this;
            this.$refs["form"].validate(valid => {
                if (valid) {
                    let method = '';
                    let params = _.cloneDeep(this.form);
                    
                    if (this.isEdit) {
                        method = "dictItemUpdateItemName";  
                    } else {
                        method = "dictItemInsert"
                        params.dictTypeId = this.currentItem.id
                    }
                    debugger
                    appMain[method](params)
                        .then(res => {
                            if (res.data.code === 20000) {
                                self.resetForm('form');
                                self.$message.success(res.data.message);
                                self.$emit('submit', { showDialog: false})
                            } else {
                                self.$message.error(res.data.message);
                            }
                        })
                        .catch(err => {
                            console.log(err);
                        });
                } else {
                    console.log('error submit!!');
                    return false;
                }
            }); 
        },
        resetForm (formName) {
             this.$refs[formName].resetFields();
        }
    },
    watch: {
        isShow: function (newVal, oldVal) {
            console.log(newVal, 'newVal')
            if(newVal == true){
                this.editVisible = true
                this.showDialog = true
                this.form = Object.assign(this.form, this.editData) 
            } else {
                this.editVisible = false
            }
        },
        editVisible: function(newVal) {
            if (!newVal) {
                this.$emit('close', {
                    showDialog: newVal
                })
            }
            
        }
    },
    computed: {
        a () {
            return 123
        }
    },
    created () {

    }

}
</script>

<style scoped>

</style>